import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

class ConfirmWithCancelButton {
  // 事件布局
  static actionsLayout(
    BuildContext context,
    RxBool availableConfirmBtn, {
    double lineTop = 0,
    bool showCancel = true,
    String confirmTitle = "确认",
    String cancelTitle = "取消",
    Color? confirmColor,
    Color? confirmColorUnvailable,
    Color cancelColor = WhiteColor.white1,
    LxFontWeightEnum confirmFontWeight = LxFontWeightEnum.titleMedium,
    LxFontWeightEnum cancelFontWeight = LxFontWeightEnum.titleMedium,
    Function? confirmCallback,
    Function? cancelCallback,
    LxAlertActionsAlignment actionsAlignment = LxAlertActionsAlignment.row,
  }) {
    confirmColor ??= LxWidgetsLibraryService.to.colorType.color6;
    bool isColumn =
        actionsAlignment == LxAlertActionsAlignment.column && showCancel;

    // 布局
    if (isColumn) {
      return Column(
        children: [
          Padding(
            padding: EdgeInsets.only(top: lineTop),
            child: _hLine(),
          ),
          _confirmButton(
            context,
            availableConfirmBtn,
            confirmTitle,
            confirmColor,
            confirmColorUnvailable,
            confirmFontWeight,
            confirmCallback: confirmCallback,
          ),
          _hLine(),
          _cancelButton(
            context,
            cancelTitle,
            cancelColor,
            cancelFontWeight,
            cancelCallback: cancelCallback,
          ),
        ],
      );
    } else {
      return Column(
        children: [
          Padding(
            padding: EdgeInsets.only(top: lineTop),
            child: _hLine(),
          ),
          SizedBox(
            height: 56.h,
            child: Row(
              children: [
                Visibility(
                  visible: showCancel == true,
                  child: Expanded(
                    child: _cancelButton(
                      context,
                      cancelTitle,
                      cancelColor,
                      cancelFontWeight,
                      cancelCallback: cancelCallback,
                    ),
                  ),
                ),
                Visibility(
                  visible: showCancel == true,
                  child: _sLine(),
                ),
                Expanded(
                  child: _confirmButton(
                    context,
                    availableConfirmBtn,
                    confirmTitle,
                    confirmColor,
                    confirmColorUnvailable,
                    confirmFontWeight,
                    confirmCallback: confirmCallback,
                  ),
                ),
              ],
            ),
          )
        ],
      );
    }
  }

  // 确认按钮
  static _confirmButton(
    BuildContext context,
    RxBool availableConfirmBtn,
    String confirmTitle,
    Color confirmColor,
    Color? confirmColorUnvailable,
    LxFontWeightEnum confirmFontWeight, {
    Function? confirmCallback,
  }) {
    return LxButton(
      title: confirmTitle,
      height: 56.h,
      buttonAlignmentEnum: ButtonAlignmentEnum.block,
      borderRadius: const BorderRadius.all(Radius.circular(0)),
      fontWeightEnum: confirmFontWeight,
      buttonTypeCustomColor: ButtonTypeColor()
        ..backgroundColor = Colors.transparent
        ..borderColor = Colors.transparent
        ..textColor =
            availableConfirmBtn.value ? confirmColor : confirmColorUnvailable!
        ..clickColor = GrayColor.gray4,
      onTap: () {
        if (availableConfirmBtn.value) {
          if (confirmCallback != null) {
            confirmCallback();
          }
        }
      },
    );
  }

  // 取消按钮
  static _cancelButton(
    BuildContext context,
    String cancelTitle,
    Color cancelColor,
    LxFontWeightEnum cancelFontWeight, {
    Function? cancelCallback,
  }) {
    return LxButton(
      title: cancelTitle,
      height: 56.h,
      buttonAlignmentEnum: ButtonAlignmentEnum.block,
      borderRadius: const BorderRadius.all(Radius.circular(0)),
      fontWeightEnum: cancelFontWeight,
      buttonTypeCustomColor: ButtonTypeColor()
        ..backgroundColor = Colors.transparent
        ..borderColor = Colors.transparent
        ..textColor = cancelColor
        ..clickColor = GrayColor.gray4,
      onTap: () {
        if (cancelCallback != null) {
          cancelCallback();
        }
      },
    );
  }

  // 横线
  static _hLine() {
    return Divider(height: 0.5.h, color: GrayColor.gray3);
  }

  // 竖线
  static _sLine() {
    return Container(width: 0.5.h, color: GrayColor.gray3);
  }
}
